package com.stylefeng.guns.modular.restDay.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.DoorRestMessage;
import com.stylefeng.guns.modular.restDay.service.IDoorRestMessageService;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 休息信息控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 14:31:35
 */
@Controller
@RequestMapping("/doorRestMessage")
public class DoorRestMessageController extends BaseController {
    private Logger log =  LoggerFactory.getLogger(this.getClass());
    private String PREFIX = "/restDay/doorRestMessage/";

    @Autowired
    private IDoorRestMessageService doorRestMessageService;

    /**
     * 跳转到休息信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "doorRestMessage.html";
    }

    /**
     * 跳转到添加休息信息
     */
    @RequestMapping("/doorRestMessage_add")
    public String doorRestMessageAdd() {
        return PREFIX + "doorRestMessage_add.html";
    }

    /**
     * 跳转到修改休息信息
     */
    @RequestMapping("/doorRestMessage_update/{doorRestMessageId}")
    public String doorRestMessageUpdate(@PathVariable Integer doorRestMessageId, Model model) {
        DoorRestMessage doorRestMessage = doorRestMessageService.selectById(doorRestMessageId);
        model.addAttribute("item",doorRestMessage);
        LogObjectHolder.me().set(doorRestMessage);
        return PREFIX + "doorRestMessage_edit.html";
    }

    /**
     * 获取休息信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String restName, String dateTime,String startTime,String endTime) {
        log.info("========restName========"+restName);
        log.info("=============dateTime==========="+dateTime);
        log.info("=============startTime==========="+startTime);
        log.info("=============endTime==========="+endTime);
//        EntityWrapper<DoorRestMessage> wrapper = new EntityWrapper<DoorRestMessage>();
//        if ((restName==null || restName=="") && (dateTime==null||dateTime=="")&&(startTime== null || startTime =="")&&(endTime==null || endTime=="")) {
//            return doorRestMessageService.selectList(null);
//        }else {
//            if (restName!=null&&restName!=""){
//                wrapper.like("restName", restName);
//            }
//            if (dateTime!=""&&dateTime!=null){
//                wrapper.and("datediff(dateTime,'"+dateTime+"')=0");
//            }
//            log.info("++++++++++++开始判断+++++++++++++++++++++++++++++++");
//            log.info("+++++++++++++++++++++++++++++++++++++++++++"+(startTime!=null));
//            if (startTime!= null && startTime !="") {
//                if (endTime!=null && endTime!=""){
//                    log.info("+++++++++++++++++++++++++++++++++++++++++++");
//                    wrapper.between("dateTime","STR_TO_DATE('"+startTime+"','%Y-%m-%d')","STR_TO_DATE('"+endTime+"','%Y-%m-%d')");
//                }
//            }
//            return doorRestMessageService.selectList(wrapper);
//        }
            return  doorRestMessageService.getRestMessageList(restName, dateTime, startTime, endTime);
    }

    /**
     * 新增休息信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DoorRestMessage doorRestMessage) {
        doorRestMessageService.insert(doorRestMessage);
        return SUCCESS_TIP;
    }

    /**
     * 删除休息信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer doorRestMessageId) {
        doorRestMessageService.deleteById(doorRestMessageId);
        return SUCCESS_TIP;
    }

    /**
     * 修改休息信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DoorRestMessage doorRestMessage) {
        doorRestMessageService.updateById(doorRestMessage);
        return SUCCESS_TIP;
    }

    /**
     * 休息信息详情
     */
    @RequestMapping(value = "/detail/{doorRestMessageId}")
    @ResponseBody
    public Object detail(@PathVariable("doorRestMessageId") Integer doorRestMessageId) {
        return doorRestMessageService.selectById(doorRestMessageId);
    }
}
